package cn.neusoft.service.impl;

import cn.neusoft.domain.Dept;
import cn.neusoft.domain.Substation;
import cn.neusoft.enums.Constants;
import cn.neusoft.mapper.SubstationMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.DeptService;
import cn.neusoft.service.SubstationService;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SubstationServiceImpl implements SubstationService {

    @Autowired
    DeptService deptService;

    @Autowired
    SubstationMapper substationMapper;

    @Override
    @Transactional
    public void add(Substation substation) {
        Dept dept = copyInfoToDept(substation);
        deptService.add(dept);

        substation.setSubstation_id(dept.getDept_id());
        substationMapper.add(substation);
    }

    @Override
    @Transactional
    public void deleteById(Long substation_id) {
        deptService.deleteById(substation_id);
        substationMapper.deleteById(substation_id);
    }

    @Override
    @Transactional
    public void update(Substation substation) {
        Dept dept = copyInfoToDept(substation);
        deptService.update(dept);

        substationMapper.update(substation);
    }

    @Override
    @Transactional
    public Substation findById(Long substation_id) {
        return substationMapper.findById(substation_id);
    }

    @Override
    @Transactional
    public List<Substation> findAll() {
        return substationMapper.findAll();
    }

    @Override
    @Transactional
    public PageBean<Substation> findByPage(BaseQuery query) {
        // 构建结果对象
        PageBean<Substation> pageBean = new PageBean<>();

        // 开启分页
        PageHelper.startPage(query.getPageNow(), query.getPageSize());

        // 查询得到当前页的集合
        List<Substation> substationList = substationMapper.findByCondition(query);

        // 创建一个分页的实体PageInfo
        PageInfo<Substation> pageInfo = new PageInfo<>(substationList);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;
    }

    @Override
    @Transactional
    public List<String> getDistinctAddress() {
        return substationMapper.getDistinctAddress();
    }

    private Dept copyInfoToDept(Substation substation){

        Dept dept = new Dept();
        dept.setDept_id(substation.getSubstation_id());
        dept.setDept_name(substation.getSubstation_name());
        dept.setParent_id(substation.getParent_id());
        dept.setDept_addr(substation.getSubstation_addr());
        dept.setLeader(substation.getLeader());
        dept.setPhone(substation.getSubstation_tel());
        dept.setStatus(substation.getStatus());
        dept.setCreate_by(substation.getCreate_by());
        dept.setUpdate_by(substation.getUpdate_by());

        return dept;
    }
}
